﻿@{
    Layout = "Admin_Layout";
    ViewData["Title"] = "联系方式";
}
@section Style {
    <style>
        .mydiv .el-form .el-form-item .el-form-item__content, .el-form-item__label {
            font-size: 15px;
            color: black;
            height: 10px;
            margin-bottom: 5px;
        }

        .mydiv .form-class .el-input__inner {
            width: 350px;
            height: 30px;
            padding: 4px;
        }

        .myTextarea .el-textarea__inner{
            border-radius:5px;
        }
    </style>
}
<div style="width:600px;" class="mydiv">
    <div style="margin-left:20px;margin-bottom:20px;">
        <div style="display:flex;width:400px;">
            <el-form ref="form" :model="form" :rules="rules" label-width="88px" class="form-class">
                <el-form-item prop="shopName" label="店铺名称">
                        <el-input maxlength="20" v-model.trim="form.shopName"></el-input>
                    </el-form-item>
                <el-form-item prop="address" label="地址">
                        <el-input maxlength="255" v-model.trim="form.address"></el-input>
                    </el-form-item>
                <el-form-item prop="phone" label="电话">
                        <el-input maxlength="20" v-model.trim="form.phone" type="number"></el-input>
                    </el-form-item>
                <el-form-item prop="email" label="邮箱">
                        <el-input maxlength="50" v-model.trim="form.email" ></el-input>
                    </el-form-item>
                <el-form-item prop="contact" label="联系人">
                        <el-input maxlength="20" v-model.trim="form.contact"></el-input>
                    </el-form-item>
            </el-form>
        </div>
        <div style="margin-top:6px;margin-bottom:20px;display:flex;">
            <div style="float:left;width:76px;text-align:right;padding-top:4px;">简介</div>
            <div style="float:left;margin-left:12px;">
                <el-input maxlength="50" v-model.trim="form.introduce" type="textarea" resize="none" class="myTextarea" style="width:488px;" :autosize="{ minRows:6, maxRows: 6}"></el-input>
            </div>
        </div>
    </div>
    <div style="margin-top:20px;">
        <div style="float:left;margin-left:20px;width:256px;">
            <div><img style="width:256px;height:160px;" :src="coverImgUrl" title="请选择图片" /></div>
            <div>
                <el-upload class="upload"
                           list-type="picture"
                           accept=".jpg , .jpeg , .png"
                           :show-file-list="false"
                           ref="upload"
                           action="string"
                           :file-list="coverImgList"
                           :auto-upload="false"
                           :http-request="uploadFile"
                           :on-change="coverHandleChange"
                           :on-preview="handlePreview"
                           :on-remove="handleRemove"
                           multiple="false">
                    <el-button slot="trigger" size="small" type="primary" @@click="coverImgFile">封面图片</el-button>
                    <div slot="tip" class="el-upload__tip"><span style="color:red;font-size:13px;margin-right:4px;">*</span>[jpg/jpeg/png图片，小于{{imgSize}}K]</div>
                </el-upload>
            </div>
        </div>
        <div style="float:left;margin-left:20px;">
            <div>
                <video width="300" height="160" controls :src="videoUrl" style="background-color:silver;">
                    <source type="video/mp4">
                    <source type="video/avi">
                    <source type="video/webm">
                    您的浏览器不支持 video 属性。
                </video>
            </div>
            <div>
                <el-upload class="upload"
                           accept=".mp4 , .webm"
                           :show-file-list="false"
                           action="string"
                           :file-list="videoList"
                           :auto-upload="false"
                           :on-change="videoHandleChange"
                           multiple="false">
                    <el-button slot="trigger" size="small" type="primary" @@click="videoFile">宠物视频</el-button>
                    <div slot="tip" class="el-upload__tip">[mp4/avi/webm视频，小于{{videoSize}}M]</div>
                </el-upload>
            </div>
        </div>
    </div>
    <div style="clear:both;"></div>
    <div style="margin-left:20px;margin-top:20px;">
        <div>
            <div style="float:left;"><img style="width:185px;height:160px;" :src="otherImgUrl1" title="请选择图片" /></div>
            <div style="float:left;margin-left:10px;"><img style="width:185px;height:160px;" :src="otherImgUrl2" title="请选择图片" /></div>
            <div style="float:left;margin-left:10px;"><img style="width:185px;height:160px;" :src="otherImgUrl3" title="请选择图片" /></div>
        </div>
        <div style="clear:both;"></div>
        <div>
            <el-upload class="upload"
                       :limit="3"
                       :on-exceed="otherHandleExceed"
                       accept=".jpg , jpeg , .png"
                       :show-file-list="false"
                       list-type="picture"
                       ref="upload"
                       action="string"
                       :auto-upload="false"
                       :file-list="otherImgList"
                       :on-change="otherHandleChange"
                       multiple="true">
                <el-button slot="trigger" size="small" type="primary" @@click="otherImgFile">其他图片</el-button>
                <div slot="tip" class="el-upload__tip">[jpg/jpeg/png图片，每张小于{{imgSize}}K]</div>
            </el-upload>
        </div>
    </div>
    <div style="background-color:black;height:1px; border:none;margin-top:15px;"></div>
    <div style="margin-bottom:15px;margin-top:15px;display:none;" id="progressDiv">
        <el-row style="margin-top:8px;display:flex;" :gutter="15">
            <el-col :span="4" style="text-align:right;margin-left:20px;font-size:15px;">
                提交进度
            </el-col>
            <el-col :span="18">
                <el-progress :text-inside="true" :stroke-width="20" :percentage="percentage"></el-progress>
            </el-col>
        </el-row>
    </div>
    <div style="margin-left:20px;width:580px;margin-top:15px;">
        <el-button type="primary" :disabled="submitBtnDisabled" @@click="submitForm('form')" style="width:110px;">确 定</el-button>
        <el-button @@click="clearForm" style="width:110px;">清 空</el-button>
    </div>
</div>
@section Scripts {
    <script>
        var app = new Vue({
            el: "#app",
            mounted: function () {
                this.loadData();
                $("#app").show();//vue渲染完后显示界面
            },
            methods: {
                async loadData() {
                    await axios.post('../../Admin/AdminHome/GetAboutUS', {
                    }).then(async res => {
                        if (res.data === null || res.data === undefined)
                            return;

                        await this.restoreData(res.data);
                    }, error => {
                        alert("错误提示：" + error.message);
                    });
                },
                async restoreData(data) {
                    this.form.aboutusid = data.aboutusid;
                    this.form.shopName = data.merchantname;
                    this.form.address = data.address;
                    this.form.phone = data.phone;
                    this.form.email = data.email;
                    this.form.contact = data.contact;
                    this.form.introduce = data.introduce;

                    if (data.coverimgsrc !== '' && data.coverimgsrc !== null) {
                        var vueFileObj = await this.getVueFile("../../Admin/AdminHome/DownFile", data.coverimgsrc);
                        this.coverImgList = [];
                        this.coverImgList.push(vueFileObj);
                        this.coverImgUrl = URL.createObjectURL(vueFileObj.raw);
                    }

                    this.videoUrl = '';
                    this.videoList = [];
                    if (data.videosrc !== '' && data.videosrc !== null) {
                        this.videoUrl = "/file/" + this.theDomain + "/videos/" + data.videosrc;
                    }

                    if (data.otherimgsrc !== '' && data.otherimgsrc !== null) {
                        app.otherImgList = [];

                        let imgUrls = data.otherimgsrc.split('|');
                        for (var i = 0; i < imgUrls.length; i++) {
                            var imgurl = imgUrls[i];

                            var vueFileObj = await app.getVueFile("../../Admin/AdminHome/DownFile", imgurl);
                            app.otherImgList.push(vueFileObj);

                            if (i === 0)
                                app.otherImgUrl1 = URL.createObjectURL(vueFileObj.raw);
                            else if (i === 1)
                                app.otherImgUrl2 = URL.createObjectURL(vueFileObj.raw);
                            else if (i === 2)
                                app.otherImgUrl3 = URL.createObjectURL(vueFileObj.raw);
                        }
                    }

                },
                otherImgFile() {
                    if (app.otherImgList.length < 3)
                        return;
                    app.otherImgList = [];
                    app.otherImgUrl1 = '/img.jpg';
                    app.otherImgUrl2 = '/img.jpg';
                    app.otherImgUrl3 = '/img.jpg';
                },
                otherHandleExceed(files, fileList) {
                    this.$message.warning('只能选择三张图片');
                },
                coverHandleChange(file, fileList) {

                    let isLt10M = file.size / 1024 < this.imgSize;
                    if (!isLt10M) {
                        this.$message.error("图片大小不能超过" + this.imgSize + "KB!");
                        return;
                    }

                    // 限制类型只能为jpg,png
                    let isJPG = file.raw.type === 'image/jpeg';
                    let isPng = file.raw.type === 'image/png';
                    if (!isJPG && !isPng) {
                        this.$message.error("请上传正确格式的图片!");
                        return;
                    }

                    if (fileList.length > 0) {
                        app.coverImgList = [fileList[fileList.length - 1]]//这一步，是 展示最后一次选择文件

                        let URL = window.URL || window.webkitURL;
                        app.coverImgUrl = URL.createObjectURL(app.coverImgList[0].raw);
                    }
                },
                videoHandleChange(file, fileList) {

                    let isLt10M = file.size / 1024 / 1024 < this.videoSize;
                    if (!isLt10M) {
                        this.$message.error("视频大小不能超过 " + this.videoSize + "MB!");
                        return;
                    }

                    // 限制类型
                    let isMp4 = file.raw.type === 'video/mp4';
                    let isAvi = file.raw.type === 'video/avi';
                    let isWebm = file.raw.type === 'video/webm';
                    if (!isMp4 && !isAvi && !isWebm) {
                        this.$message.error('请上传正确格式的视频');
                        return;
                    }

                    let URL = window.URL || window.webkitURL;

                    if (fileList.length > 0) {
                        app.videoList = [fileList[fileList.length - 1]]//这一步，是 展示最后一次选择文件
                        app.videoUrl = URL.createObjectURL(app.videoList[0].raw);
                    }
                },
                otherHandleChange(file, fileList) {

                    let isLt10M = file.size / 1024 < this.imgSize;
                    if (!isLt10M) {
                        this.$message.error("图片大小不能超过" + this.imgSize + "KB!");
                        return;
                    }

                    // 限制类型只能为jpg,png
                    let isJPG = file.raw.type === 'image/jpeg';
                    let isPng = file.raw.type === 'image/png';
                    if (!isJPG && !isPng) {
                        this.$message.error("请上传正确格式的图片!");
                        return;
                    }

                    let URL = window.URL || window.webkitURL;
                    app.otherImgList = fileList;
                    for (var i = 0; i < app.otherImgList.length; i++) {
                        if (i === 0) {
                            app.otherImgUrl1 = URL.createObjectURL(app.otherImgList[i].raw);
                        }
                        else if (i === 1) {
                            app.otherImgUrl2 = URL.createObjectURL(app.otherImgList[i].raw);
                        }
                        else if (i === 2) {
                            app.otherImgUrl3 = URL.createObjectURL(app.otherImgList[i].raw);
                        }
                    }
                },
                clearInfo() {
                    app.form.shopName = '';
                    app.form.address = '';
                    app.form.phone = '';
                    app.form.email = '';
                    app.form.contact = '';
                    app.form.introduce = '';

                    app.coverImgList = [];
                    app.videoList = [];
                    app.otherImgList = [];
                    app.coverImgUrl = '/img.jpg';
                    app.videoUrl = '';
                    app.otherImgUrl1 = '/img.jpg';
                    app.otherImgUrl2 = '/img.jpg';
                    app.otherImgUrl3 = '/img.jpg';
                },
                uploadProgress(progressEvent) {
                    /*
                     * progressEvent.loaded :已上传量
                     * progressEvent.total :上传的总大小
                    */
                    const p = Math.floor((progressEvent.loaded / progressEvent.total) * 100);
                    this.percentage = p;
                },
                //提交窗口
                async submitForm(formName) {
                    if (this.form.selectedSpeciesItem === '请选择') {
                        this.form.selectedSpeciesItem = '';
                    }

                    this.$refs[formName].validate(async (valid) => {
                        if (valid) {

                            //自定义验证
                            if (this.coverImgList.length === 0) {
                                this.$message.warning('封面图片不能为空');
                                return;
                            }

                            let formData = new FormData();

                            //封面图片
                            for (var i = 0; i < this.coverImgList.length; i++) {
                                formData.append("coverImg" + i, this.coverImgList[i].raw);
                            }

                            //其他图片
                            for (var i = 0; i < this.otherImgList.length; i++) {
                                formData.append("otherImg" + i, this.otherImgList[i].raw);
                            }

                            //视频
                            for (var i = 0; i < this.videoList.length; i++) {
                                formData.append("video" + i, this.videoList[i].raw);
                            }

                            //文字内容
                            var textData = {
                                "aboutusid": app.form.aboutusid, "shopName": app.form.shopName, "address": app.form.address, "email": app.form.email, "phone": app.form.phone,
                                "contact": app.form.contact, "introduce": app.form.introduce
                            };
                            formData.append("textData", JSON.stringify(textData));

                            let config = {
                                headers: {
                                    'Content-Type': 'multipart/form-data;charset=utf-8',
                                },
                                transformRequest: [function (data) {
                                    return data
                                }],
                                onUploadProgress: this.uploadProgress,
                            }

                            $("#progressDiv").show();
                            this.submitBtnDisabled = true;

                            await axios.post('../../Admin/AdminHome/AddOrUpdateContactInfo', formData, config).then(res => {
                                if (res.data === "ok") {
                                    this.$message.success('提交成功');
                                    this.percentage = 0;
                                    $("#progressDiv").hide();
                                    this.submitBtnDisabled = false;
                                    this.clearInfo();
                                    this.loadData();
                                } else {
                                    this.percentage = 0;
                                    $("#progressDiv").hide();
                                    this.submitBtnDisabled = false;
                                    this.$message.warning(res.data);
                                }
                            }, error => {
                                this.percentage = 0;
                                $("#progressDiv").hide();
                                this.submitBtnDisabled = false;
                                alert("错误提示：" + error.message);
                            });


                        } else {
                            console.log('error submit!');
                            return false;
                        }
                    });
                },
            },
            data() {
                return {
                    theDomain: '@Html.Raw(ViewData["theDomain"])',
                    submitBtnDisabled: false,
                    percentage: 0,
                    videoSize: @Html.Raw(ViewData["videoSize"]),
                    imgSize: @Html.Raw(ViewData["imgSize"]),
                    defaultMenuActive: '4',
                    coverImgList: [],
                    videoList: [],
                    otherImgList: [],
                    coverImgUrl: '/img.jpg',
                    videoUrl: '',
                    otherImgUrl1: '/img.jpg',
                    otherImgUrl2: '/img.jpg',
                    otherImgUrl3: '/img.jpg',
                    form: {
                        aboutusid: -1,
                        shopName: '',
                        address: '',
                        phone: '',
                        email: '',
                        contact: '',
                        introduce: '',
                    },
                    rules: {
                        shopName: [
                            { required: true, message: '√', trigger: 'blur' }
                        ],
                        address: [
                            { required: true, message: '√', trigger: 'blur' }
                        ],
                        phone: [
                            { required: true, message: '√', trigger: 'blur' }
                        ],
                        email: [
                            { required: true, message: '√', trigger: 'blur' }
                        ],
                        contact: [
                            { required: true, message: '√', trigger: 'blur' }
                        ],
                    },
                }
            }
        });
    </script>
}
